package com.bookstore.backend.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bookstore.backend.model.ResultVO;
import com.bookstore.backend.model.entity.User;
import com.bookstore.backend.model.req.UserListReq;
import com.bookstore.backend.service.UserService;
//import com.sun.org.apache.xpath.internal.operations.Bool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author: world
 * @date: 2022/7/29 15:10
 * @description: 测试接口
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    //get请求参数的接收，用实体类接收参数
    public ResultVO<User> login(@RequestParam(value = "username", defaultValue = "") String user, @RequestParam(value = "password", defaultValue = "") String password) {
        final ResultVO<User> login = userService.login(user, password);
        if (login.getData() != null) {
            System.err.println(login.getData().getPassword());
        }
        return login;
    }

    @RequestMapping(value = "/signIn", method = RequestMethod.POST)
//POST传递参数，接收参数
    public ResultVO<User> signup(@RequestParam(value = "username", defaultValue = "") String user,
                                 @RequestParam(value = "password", defaultValue = "") String password,
                                 @RequestParam(value = "phone",defaultValue = "")String phone,
                                 @RequestParam(value = "email",defaultValue = "")String email,
                                 @RequestParam(value = "click",defaultValue = "") Boolean click) {
        final ResultVO<User> signup = userService.signup(user, password,phone,email,click);
        return signup;
    }

    // 查询用户列表
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public ResultVO<Object> list(UserListReq listReq){
        return ResultVO.success(userService.list(listReq));
    }

    //分页查询
    @RequestMapping(value = "/page",method = RequestMethod.GET)
    public ResultVO<Page<User>> tableController(@RequestParam(value = "page",defaultValue = "1")Integer page, @RequestParam(value = "size",defaultValue = "4")Integer size){
        Page<User> userPage = new Page<User>(page,size);
        Page<User> result = userService.page(userPage);
        return ResultVO.success(result);
    }


    // 更新用户
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public ResultVO<?> updateNumByuid(@RequestBody User user){return ResultVO.success(userService.updateNumByuid(user));}

    // 删除用户
    @RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    public  ResultVO<?> deleteNumByuid(Integer userId){
        return ResultVO.success(userService.deleteNumByuid(userId));
    }

    // 添加用户
    @PostMapping("/insert")
    public ResultVO<?> insert(@RequestBody User user){
        return ResultVO.success(userService.insert(user));
    }
}